package com.woniuxy.springdataredis.service.impl;

import com.woniuxy.springdataredis.entity.User;
import com.woniuxy.springdataredis.mapper.UerMapper;
import com.woniuxy.springdataredis.service.UserService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * description: UserServiceImpl <br>
 * date: 2022/1/6 1:17 下午 <br>
 * author: shesaifei <br>
 * version: 1.0 <br>
 */

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UerMapper uerMapper;

    @Override
    @Cacheable("findAll")
    public List<User> findAll() {
        return uerMapper.findAll();
    }

    @Override
    @Cacheable(value = "findById", key = "#id")
    public User findById(int id) {
        System.out.println("service调用mapper获取数据");
        return uerMapper.findById(id);
    }

    @Override
    //@CacheEvict(value = {"findById","findAll"}, allEntries = true)
    @Caching(evict = {
            @CacheEvict(value = "findAll", allEntries = true),
            @CacheEvict(value = "findById", key = "#id")
    })
    public boolean delById(int id) {
        //假设删除数据库成功了 redis的数据是不是要删除
        return uerMapper.delById(id) > 0 ? true : false;
    }
}
